package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.AreaInfoQry;
import com.jftone.bh.app.model.NtbBase;
import com.jftone.bh.app.model.NtbUser;
import com.jftone.bh.app.model.NtbVipCard;
import com.jftone.bh.app.model.SysArea;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import java.util.List;


@Service
public class VipOrderService {
	
	private Logger logger = Logger.getLogger(VipOrderService.class);
	
	@Autowired
	private Dao dao;

	public List<IData<String, Object>> queryList(IData<String, Object> data, Page page) throws Exception {
		List<IData<String, Object>> list = null;
		String userId = data.getString("userId", "");
		String orderNo = data.getString("orderNo", "");
		String status = data.getString("status", "");
		String cardId = data.getString("cardId", "");
		String sTime = data.getString("sTime", "");
		String eTime = data.getString("eTime", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(userId)){
			param.put("USER_ID", userId);
		}
		if(!StringUtil.isBlank(orderNo)){
			param.put("ORDER_NO", orderNo);
		}
		if(!StringUtil.isBlank(status)){
			param.put("STATUS", status);
		}
		if(!StringUtil.isBlank(cardId)){
			param.put("CARD_ID", cardId);
		}
		if(!StringUtil.isBlank(sTime)){
			param.put("S_TIME", DateUtil.getMillisecond(DateUtil.parseDate(sTime)));
		}
		if(!StringUtil.isBlank(eTime)){
			param.put("E_TIME", DateUtil.getMillisecond(DateUtil.parseDate(eTime)));
		}
		list = dao.query("queryVipOrderList", param, page);
		param.clear();
		NtbUser ntbUser;
		NtbVipCard ntbVipCard;
		NtbBase base;
		SysArea sysArea;
		for (IData<String, Object> obj:list) {
			param.clear();
			param.put("id",obj.getInt("USER_ID"));
			ntbUser = dao.get(NtbUser.class,param);
			if(ntbUser != null){
				obj.put("ACCOUNT", ntbUser.getAccount());
				obj.put("USER_NAME", ntbUser.getUserName());
			}
			param.clear();
			param.put("id",obj.getInt("CARD_ID"));
			ntbVipCard = dao.get(NtbVipCard.class,param);
			if(ntbVipCard != null){
				obj.put("CARD_NAME", ntbVipCard.getCardName());
				obj.put("AFFILIATION_NAME", "");
				if(!StringUtil.isEmpty(obj.getString("AFFILIATION",""))){
					if(ntbVipCard.getCardType()== SysConst.CARD_2){
						param.clear();
						param.put("id",obj.getInt("AFFILIATION",0));
						base = dao.get(NtbBase.class,param);
						if(base != null){
							obj.put("AFFILIATION_NAME", base.getBname());
						}
					}else if(ntbVipCard.getCardType()== SysConst.CARD_3){
						obj.put("AFFILIATION_NAME", AreaInfoQry.getAreaName("0",obj.getString("AFFILIATION","")));
					}
				}

			}
		}
		return list;
	}

	public List<NtbVipCard> queryNtbVipCardList() throws DaoException {
		return dao.query(NtbVipCard.class,null);
	}
}
